package com.onlyphoto.onlymall.mvp.presenter

import android.content.Context
import com.onlyphoto.onlymall.base.BaseNetworkResponse
import com.onlyphoto.onlymall.base.BaseObserver

import com.onlyphoto.onlymall.mvp.contract.COrderConfirm
import com.onlyphoto.onlymall.mvp.model.MOrderConfirmImpl
import com.onlyphoto.onlymall.base.BasePresenter
import com.onlyphoto.onlymall.bean.GoodsSimpleBean
import com.onlyphoto.onlymall.bean.response.AddressBean
import com.onlyphoto.onlymall.bean.response.CouponBean
import com.onlyphoto.onlymall.bean.response.FreightBean
import com.onlyphoto.onlymall.bean.response.OrderSuccessBean


class POrderConfirmImpl(mContext: Context, mView: COrderConfirm.IVOrderConfirm) : BasePresenter<COrderConfirm.IVOrderConfirm, MOrderConfirmImpl>(mContext, mView, MOrderConfirmImpl()), COrderConfirm.IPOrderConfirm {
    override fun getAddress() {
        mModel.requestUserAddress(object : BaseObserver<ArrayList<AddressBean>>(mContext.get()){
            override fun onSuccess(data: ArrayList<AddressBean>?, msg: String?) {
                if (data == null) {
                    mView.emptyAddress()
                }
                mView.getAddressSuccess(data!!)
            }

            override fun onCodeError(t: BaseNetworkResponse<ArrayList<AddressBean>>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onRequestStart() {
            }

            override fun onFinally() {
            }

        })
    }

    override fun getFreight(list: ArrayList<GoodsSimpleBean>) {
        mModel.requestFreight(list, object :BaseObserver<FreightBean>(mContext.get()){
            override fun onSuccess(data: FreightBean?, msg: String?) {
                mView.getFreightSuccess(data!!)
            }

            override fun onCodeError(t: BaseNetworkResponse<FreightBean>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onRequestStart() {
            }

            override fun onFinally() {
            }

        })
    }

    override fun getUserCoupons(get_type: Int, status: Int) {
        mModel.getUserCoupons(object :BaseObserver<ArrayList<CouponBean>>(mContext.get()){
            override fun onSuccess(data: ArrayList<CouponBean>?, msg: String?) {
                mView.getCouponsSuccess(data!!)
            }

            override fun onCodeError(t: BaseNetworkResponse<ArrayList<CouponBean>>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onRequestStart() {
            }

            override fun onFinally() {
            }

        }, get_type, status)
    }

    override fun createOrder() {
        val bean = mView.getPayBean()
        if (bean.user_address_id == 0) {
            mView.showToast("地址不能为空")
            return
        }
        mModel.pay(object :BaseObserver<OrderSuccessBean>(mContext.get()){
            override fun onSuccess(data: OrderSuccessBean?, msg: String?) {
                mView.orderCreateSuccess(data!!)
            }

            override fun onCodeError(t: BaseNetworkResponse<OrderSuccessBean>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onRequestStart() {
            }

            override fun onFinally() {
            }

        }, bean)
    }
}
